{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 控制语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "for k in range(0,2):\n",
    "    print(k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "badminton\n"
     ]
    }
   ],
   "source": [
    "weather = \"rainy\"\n",
    "if weather == \"sunny\":\n",
    "    print(\"basketball\")\n",
    "else:\n",
    "    print(\"badminton\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "He\n"
     ]
    }
   ],
   "source": [
    "a = 0\n",
    "if a == 1 :\n",
    "    print(\"She\")\n",
    "else:\n",
    "    print(\"He\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, world\n",
      "0\n",
      "Hello, world\n",
      "1\n",
      "Hello, world\n",
      "2\n",
      "Hello, world\n",
      "3\n",
      "Hello, world\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print(\"Hello, world\")\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, world\n",
      "5\n",
      "Hello, world\n",
      "4\n",
      "Hello, world\n",
      "3\n",
      "Hello, world\n",
      "2\n",
      "Hello, world\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "i=5\n",
    "while i:\n",
    "    print(\"Hello, world\")\n",
    "    print(i)\n",
    "    i=i-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = 10\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "if a == 10:\n",
    "    print(\"a = 10\")\n",
    "elif a > 8:\n",
    "    print(\"a > 8\")\n",
    "elif a > 6:\n",
    "    print(\"a > 6\")\n",
    "else:\n",
    "    print(\"a <= 6\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "cao\n",
      "bin\n",
      "xin\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for i in [1, 2, \"cao\", \"bin\", \"xin\", \"3\"]:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "4\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 10, 3):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 在python 中， for ... in 语句经常和 迭代器 组合使用， 迭代器 指任何可以进行迭代操作的对象，比如前面提到的列表、字符串、元组、字典、文件等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c\n",
      "a\n",
      "o\n",
      "b\n",
      "i\n",
      "n\n",
      "x\n",
      "i\n",
      "n\n"
     ]
    }
   ],
   "source": [
    "for i in \"caobinxin\":\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "b\n",
      "c\n",
      "d\n"
     ]
    }
   ],
   "source": [
    "for i in ('a', 'b','c','d'):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你\n",
      "好\n",
      "世\n",
      "界\n",
      "元组：你好\n",
      "元组：世界\n",
      "列表:你好\n",
      "列表:世界\n",
      "first\n",
      "字典：你好\n",
      "first=字典：你好\n",
      "second\n",
      "字典：世界\n",
      "second=字典：世界\n"
     ]
    }
   ],
   "source": [
    "char = u\"你好世界\" # u 代表这是 Unicode 编码\n",
    "tem = (\"元组：你好\", \"元组：世界\")\n",
    "list = [\"列表:你好\", \"列表:世界\"]\n",
    "dict = {\"first\":\"字典：你好\", \"second\":\"字典：世界\"}\n",
    "\n",
    "for i in char:\n",
    "    print(i)\n",
    "    \n",
    "for i in tem:\n",
    "    print(i)\n",
    "    \n",
    "for i in list:\n",
    "    print(i)\n",
    "\n",
    "for i in dict:\n",
    "    print(i)\n",
    "    print(dict[i])\n",
    "    print(i+\"=\"+dict[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "for ... in 语句的使用要点如下：\n",
    "  - 适当运用 range、xrange 函数\n",
    "  - 在 for 中还可以再写 for, 形成循环嵌套\n",
    "  - 合理地运用 lambda 代替 for 循环，可以提高运行速度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello,world\n"
     ]
    }
   ],
   "source": [
    "a = True\n",
    "while a:\n",
    "    print(\"hello,world\")\n",
    "    a = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "b = False\n",
    "while b:\n",
    "    print(\"True\")\n",
    "else:\n",
    "    print(\"False\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "hello\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "while a < 5:\n",
    "    if a <= 3:\n",
    "        print(a)\n",
    "    else:\n",
    "        print(\"hello\")\n",
    "    a = a + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "hello, world\n",
      "2\n",
      "hello, world\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "while a:\n",
    "    print(a)\n",
    "    print(\"hello, world\")\n",
    "    a = a + 1\n",
    "    if a == 3:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "hello world\n",
      "8\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "for i in range(5, 9, 3):\n",
    "    print(i)\n",
    "    print(\"hello world\")\n",
    "    if i > 6:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6 1\n",
      "6 2\n",
      "7 1\n",
      "7 2\n",
      "8 1\n",
      "8 2\n",
      "9 1\n",
      "9 2\n"
     ]
    }
   ],
   "source": [
    "a = 5\n",
    "while a <= 8:\n",
    "    a = a + 1\n",
    "    for i in range(1, 4):\n",
    "        print(a,i)\n",
    "        if i == 2:\n",
    "            break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
